/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.filehandling.xml;

import java.util.LinkedList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import softstarter.launching.RunnableItem;


public class FileObjectDocumentHandler extends DefaultHandler {

	private List<RunnableItem> runnableItems;
	private RunnableItem runnableItem;
	private String tempValue;
	
	public List<RunnableItem> getFileObjects() {
		return runnableItems;
	}
		
    public void startDocument () {
//    	System.out.println("End document");
    	runnableItems = new LinkedList<RunnableItem>();
    }


    public void endDocument () {
//    	System.out.println("End document");
    }


    public void startElement (String uri, String name, String qName, Attributes atts) {
    	
//    	System.out.println("Start element uri:" + uri + " name:" + name + " qName:" + qName);
    	
    	if (FileListXMLTags.FILE.getTag().equals(name))
    		runnableItem = new RunnableItem("Initialized", "initialized");    		

    }

    public void characters (char ch[], int start, int length) {
    	tempValue = new String(ch, start, length);
//    	System.out.println("TEMPVALUE: " + tempValue);
    }

    public void endElement (String uri, String name, String qName) {
    	
    	if (FileListXMLTags.NAME.getTag().equals(name)) {
    		runnableItem.setName(tempValue);
    		System.out.println("-----------> " + tempValue);
    	} else if (FileListXMLTags.PATH.getTag().equals(name))
		    runnableItem.setFullPath(tempValue);
    	else if (FileListXMLTags.FILE.getTag().equals(name))
    		runnableItems.add(runnableItem);
//    	System.out.println("End element uri:" + uri + " name:" + name + " qName:" + qName + " TEMP: " + tempValue);
    }
	
}
